package com.goods.business.service.imp;

import com.goods.business.mapper.*;
import com.goods.business.service.OutStockService;
import com.goods.common.error.SystemException;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.OutStockDetailVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.system.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class OutStockServiceImpl implements OutStockService {
    @Resource
    private OutStockMapper outStockMapper;
    @Resource
    private OutStockInfoMapper outStockInfoMapper;
    @Resource
    private ConsumerMapper consumerMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private UserService userService;
    @Resource
    private ProductStockMapper productStockMapper;



    @Override
    public List<OutStockVO> findOutStockList(OutStockVO outStockVO) {

        return outStockMapper.selectByOutStockVO(outStockVO);
    }

    @Override
    public OutStockDetailVO findOutStockDetail(Long id) {
        return outStockMapper.selectOutStockDetail(id);

    }

    @Override
    public Integer removeAndBack(Long id, Integer status) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStock.setStatus(status);
        return outStockMapper.updateByPrimaryKeySelective(outStock);
    }

    @Override
    public Integer delete(Long id) {

        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        OutStockInfo outStockInfo = new OutStockInfo();
        outStockInfo.setOutNum(outStock.getOutNum());
        outStockInfoMapper.delete(outStockInfo);
        return outStockMapper.deleteByPrimaryKey(id);

    }

    @Override
    public Integer addIntoStock(OutStockVO outStockVO) {
        List<Object> products = outStockVO.getProducts();
        Long consumerId = outStockVO.getConsumerId();
        Consumer consumer = new Consumer();
        String outNum = UUID.randomUUID().toString().replaceAll("-", "");
        Integer num = 0;
        Date date = new Date();
        if (consumerId == null) {
            consumer.setName(outStockVO.getName());
            consumer.setAddress(outStockVO.getAddress());
            consumer.setPhone(outStockVO.getPhone());
            consumer.setSort(outStockVO.getSort());
            consumer.setContact(outStockVO.getContact());
            consumer.setCreateTime(date);
            consumer.setModifiedTime(date);
            consumerMapper.insert(consumer);
        }

        for (Object product : products) {
            Map map = (HashMap) product;
            String productId = map.get("productId").toString();
            Integer productNumber = Integer.parseInt(map.get("productNumber").toString());
            Product product1 = productMapper.selectByPrimaryKey(productId);
            OutStockInfo outStockInfo = new OutStockInfo();
            outStockInfo.setOutNum(outNum);
            outStockInfo.setPNum(product1.getPNum());
            outStockInfo.setProductNumber(productNumber);
            outStockInfo.setCreateTime(date);
            outStockInfo.setModifiedTime(date);
            outStockInfoMapper.insert(outStockInfo);
            ProductStock productStock = new ProductStock();
            productStock.setPNum(productId);
            num = num + productNumber;
        }
        String username = null;
        try {
            UserInfoVO info =  userService.info();
            username = info.getUsername();
        } catch (SystemException e) {
            e.printStackTrace();
        }
        OutStock outStock = new OutStock();
        outStock.setRemark(outStockVO.getRemark());
        outStock.setType(outStockVO.getType());
        outStock.setOutNum(outNum);
        outStock.setStatus(2);
        outStock.setCreateTime(date);
        outStock.setOperator(username);
        outStock.setProductNumber(num);
        outStock.setConsumerId(consumer.getId());
        outStock.setPriority(outStockVO.getPriority());
        if (consumerId != null){
            outStock.setConsumerId(consumerId);
        }
        return outStockMapper.insert(outStock);
    }

    @Override
    public Integer publish(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStock.setStatus(0);
        List<Map> list = outStockMapper.selectProductStockList(id);
        for (Map map : list) {
            ProductStock productStock = new ProductStock();
            productStock.setId(Long.parseLong(map.get("id").toString()));
            productStock.setPNum(map.get("p_num").toString());
            productStock.setStock(Long.parseLong(map.get("stock").toString()) - Long.parseLong(map.get("product_number").toString()));
            productStockMapper.updateByPrimaryKeySelective(productStock);
        }
        return outStockMapper.updateByPrimaryKeySelective(outStock);
    }
}
